<?php
    mysql_query("SET NAMES 'utf8'");

	function READ_SESSION($sessionName){
		return $_SESSION[$sessionName];
	}
       
    /*
    *	the execSQL function is used for execing a sql statement
    *	@param $sql: sql statement
    *	result: a resourse
    */
	function execSQL($sql){
		$rs = @mysql_query($sql);
		if(isset($rs)){
			return $rs;
		}
	}
    
    /*
    *	the pagging function is used for pagging data retrieved from database
    *	@param $totalRecords: total records are retrieved from database
    *	@pram $limit: limit numberic record
    *	$param $css: css for links
    *   @param $urll : url for links
    */
	function pagging($totalRecords,$offset, $limit, $url = null){	
		$totalPages = ceil($totalRecords/$limit);
		if($totalPages>1){
			$next = $offset + $limit;
			$prev = $offset - $limit;
			$pagers = "";
			$current = ($offset/$limit) + 1;
			if($current != 1){
				$pagers .="<a href='".$url."&p=".$prev."'>"."Trước"."</a>";
			}
			for($p =1; $p<=$totalPages; $p++){
				if($current != $p){
					$pagers .= "<a href = '".$url."&p=".$limit*($p-1)."'>".$p."</a>";
				}else{
					$pagers .= "<a class = 'current'>".$p."</a>";
				}
			}
			if($current != $totalPages){
				$pagers .= "<a href = '".$url."&p=".$next."'>"."Sau"."</a>";
			}
		}
		return $pagers;
	}
    
    /*
    *	totalRecords function is used to get the number of records from database
    *	@param $sql: sql statement
    *	result total record
    */
    function totalRecords($sql){
    	$rs = execSQL($sql);
    	if(isset($rs)){
    		$totalRecords = mysql_num_rows($rs);
    		return $totalRecords;
    	}else{
    		return 0;
    	}
    }
    
    /*
    *   Function shopcart
    */ 
   	function get_product_name($pid){
		$result=mysql_query("select milkname from product where id_p='" . $pid . "'");
		$row=mysql_fetch_array($result);
		return $row['milkname'];
	}
    
	function get_cost($pid){
		$result=mysql_query("select cost from product where id_p='" . $pid . "'");
		$row=mysql_fetch_array($result);
		return $row['cost'];
	}
    
	function remove_product($pid){
		$pid=intval($pid);
		$max=count($_SESSION['cart']);
		for($i=0;$i<$max;$i++){
			if($pid==$_SESSION['cart'][$i]['productid']){
				unset($_SESSION['cart'][$i]);
				break;
			}
		}
		$_SESSION['cart']=array_values($_SESSION['cart']);
	}
    
	function get_order_total(){
		$max=count($_SESSION['cart']);
		$sum=0;
		for($i=0;$i<$max;$i++){
			$pid=$_SESSION['cart'][$i]['productid'];
			$q=$_SESSION['cart'][$i]['qty'];
			$cost=get_cost($pid);
			$sum+=$cost*$q;
		}
		return $sum;
	}
    
	function addtocart($pid,$q){
		if($pid<1 or $q<1) return;
		
		if(is_array($_SESSION['cart'])){
			if(product_exists($pid)) return;
			$max=count($_SESSION['cart']);
			$_SESSION['cart'][$max]['productid']=$pid;
			$_SESSION['cart'][$max]['qty']=$q;
		}
		else{
			$_SESSION['cart']=array();
			$_SESSION['cart'][0]['productid']=$pid;
			$_SESSION['cart'][0]['qty']=$q;
		}
	}
    
	function product_exists($pid){
		$pid=intval($pid);
		$max=count($_SESSION['cart']);
		$flag=0;
		for($i=0;$i<$max;$i++){
			if($pid==$_SESSION['cart'][$i]['productid']){
				$flag=1;
				break;
			}
		}
		return $flag;
	}
    
    /*
    *   Ham cat so ky tu trong chuoi
    *   $str:   chuoi ky tu can cat
    *   $len:   so ky tu giu lai
    *   $more:  vi tri bat dau cat
    */
    
    function cut_string($str,$len,$more){
        if ($str=='' || $str==NULL) return $str;
        if (is_array($str)) return $str;
        $str = trim($str);
        if (strlen($str) <= $len) return $str;
        $str = substr($str,0,$len);
        if ($str != '') {
            if (!substr_count($str," ")) {
            if ($more) $str .= " ...";
            return $str;
            }
            while(strlen($str) && ($str[strlen($str)-1] != " ")) $str = substr($str,0,-1);
            $str = substr($str,0,-1);
            if ($more) $str .= " ...";
        }
        return $str;
    } 
?>